import setting from '@/setting.js';
import { jsonpProxy } from '@/common/jsonp.js';
// 腾讯地图WebService接口
function tencentMapWebServiceAPI(Vue, vm) {
	// jsonp 代理
    let jsonPTencentMapLocation = (lat, lng) => {
        return new Promise((resolve, reject) => {
            jsonpProxy(
                'https://apis.map.qq.com/ws/geocoder/v1/',
                {
                    key: uni.$setting.tencentMapKay,
                    location: lat + ',' + lng,
					output: "jsonp",
                },
				'callback',
				20000,
                (data) => {
                    resolve(data);
                },
                (err) => {
                    reject(err);
                }
            );
        });
    };
	// 地址解析，坐标解析详细信息
	// 经纬度（GCJ02坐标系），格式：location=lat<纬度>,lng<经度>
	let tencentMapLocation = (lat, lng) => {
		
		return new Promise((resolve, reject) => {
			uni.request({
			    url: 'https://apis.map.qq.com/ws/geocoder/v1/',
			    data: {
			        key: uni.$setting.tencentMapKay,
					location: lat + ',' + lng
			    },
			    success: (res) => {
			        resolve(res);
			    },
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	
	// 地址转坐标
	// address地址（注：地址中请包含城市名称，以及需要对地址进行URL编码，否则会影响解析效果）
	let tencentMapAddress = (address) => {
		return new Promise((resolve, reject) => {
			uni.request({
			    url: 'https://apis.map.qq.com/ws/geocoder/v1/',
			    data: {
			        key: uni.$setting.tencentMapKay,
					address: address
			    },
			    success: (res) => {
			        resolve(res);
			    },
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	
	// 地点搜索
	// 参考文档https://lbs.qq.com/service/webService/webServiceGuide/webServiceSearch
	let tencentMapPlaceSearch = (boundary, keyword, category, orderby='_distance') => {
		
		return new Promise((resolve, reject) => {
			uni.request({
			    url: 'https://apis.map.qq.com/ws/place/v1/search',
			    data: {
			        key: uni.$setting.tencentMapKay,
					get_subpois: 1,
					keyword: keyword,
					boundary: boundary,
					category: category,
					page_size: 20,
					orderby:orderby,
			    },
			    success: (res) => {
			        resolve(res);
			    },
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	// 参考文档https://lbs.qq.com/service/webService/webServiceGuide/webServiceSearch
	// boundary, keyword, category, orderby='_distance'
	// {boundary:?，keywords: ?, category:?,orderby:'_distance'}
	let tencentMapSearchPlace = (params) => {
		
		return new Promise((resolve, reject) => {
			uni.request({
			    url: 'https://apis.map.qq.com/ws/place/v1/search',
			    data: {
			        key: uni.$setting.tencentMapKay,
					get_subpois: 1,
					page_size: 20,
					orderby:'_distance',
					...params
			    },
			    success: (res) => {
			        resolve(res);
			    },
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	
	let tencentMapLocationPOIS = (lat, lng, pois=1, poi_options='radius=5000') => {
		
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'https://apis.map.qq.com/ws/geocoder/v1/',
				data: {
					key: uni.$setting.tencentMapKay,
					location: lat + ',' + lng,
					get_poi: pois,
					poi_options: poi_options
				},
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	
	let tencentMapLocationEXP = (lat, lng, filter='', radius='1000',orderby='_distance') => {
		return new Promise((resolve, reject) => {
			let boundary = 'nearby('+lat+','+lng+','+radius+',0)';
			uni.request({
				url: 'https://apis.map.qq.com/ws/place/v1/explore/',
				data: {
					key: uni.$setting.tencentMapKay,
					boundary: boundary,
					orderby: orderby,
					filter: filter,
					page_size: 20
				},
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				}
			});
		})
	};
	
	let jsonPtencentMapLocationEXP = (lat, lng, filter='', radius='1000',orderby='_distance') => {
		var res = [];
		let boundary = 'nearby('+lat+','+lng+','+radius+',0)';
		return new Promise((resolve, reject) => {
		    jsonpProxy(
		        'https://apis.map.qq.com/ws/place/v1/explore/',
		        {
		            key: uni.$setting.tencentMapKay,
		            boundary: boundary,
					orderby: orderby,
					filter: filter,
					page_size: 20,
					output: "jsonp",
		        },
				'callback',
				20000,
		        (data) => {
					res.data = data
		            resolve(res);
		        },
		        (err) => {
		            reject(err);
		        }
		    );
		});
	
	};
	
	let tencentMapRegionSearch = (region, keyword, category) => {
	    return new Promise((resolve, reject) => {
	        uni.request({
	            url: 'https://apis.map.qq.com/ws/place/v1/search',
	            data: {
	                key: uni.$setting.tencentMapKay,
	                keyword: keyword,
	                boundary: `region(${region},0)`,  // 使用region边界类型进行地区限定
	                category: category,
	                page_size: 10,
	                orderby: '_distance'  // 根据距离排序
	            },
	            success: (res) => {
	                resolve(res);
	            },
	            fail: (err) => {
	                reject(err);
	            }
	        });
	    });
	};
	
	// jsonp 代理
	let jsonPtencentMapRegionSearch = (region, keyword, category) => {
		var res = [];
	    return new Promise((resolve, reject) => {
	        jsonpProxy(
	            'https://apis.map.qq.com/ws/place/v1/search',
	            {
	                key: uni.$setting.tencentMapKay,
	                keyword: keyword,
	                boundary: `region(${region},0)`,  // 使用region边界类型进行地区限定
	                category: category,
	                page_size: 10,
	                orderby: '_distance',  // 根据距离排序
					output: "jsonp",
	            },
				'callback',
				20000,
	            (data) => {
					res.data = data
	                resolve(res);
	            },
	            (err) => {
	                reject(err);
	            }
	        );
	    });
	};
	
	return {
		tencentMapLocation,
		tencentMapAddress,
		tencentMapPlaceSearch,
		tencentMapSearchPlace,
		tencentMapLocationPOIS,
		tencentMapLocationEXP,
		tencentMapRegionSearch,
		jsonPTencentMapLocation,
		jsonPtencentMapRegionSearch
	};
}

export default tencentMapWebServiceAPI
